'use strict';

/* Filters */
angular.module('app.filters', [])
.filter( 'unixToDate', function(){
	// format  YYYY-MM-DD HH:MM:SS
	return function(timestamp,format){
		var time = new Date( timestamp * 1000), 
	    date = '',
	    month = time.getMonth() + 1,
	    day = time.getDate();
		//format
		if ( format ) {
			var dateFullArr = format.split(' ',2 ),
				dateArr = ( dateFullArr[0] ? dateFullArr[0] : '' ).split( '-',3 ),
		        timeArr = ( dateFullArr[1] ? dateFullArr[1] : '' ).split( ':',3 );
		       //年 yyyy
		       if ( angular.uppercase(dateArr[0]) == 'YYYY') {
		       		date += time.getUTCFullYear();
		       }else{
		       		date += 'YYYY';
		       }
		       //月 mm
		      	if ( angular.uppercase(dateArr[1]) == 'M') {
		       		date += "-" + month
			    }else if( angular.uppercase(dateArr[1]) == 'MM' ){
			       	date += "-" + (month < 10 ?'0' + month : month);
			    }
			    //日  dd
			    if ( angular.uppercase(dateArr[2]) == 'D') {
		       		date += "-" + day
			    }else if( angular.uppercase(dateArr[2]) == 'DD' ){
			       	date += "-" + (day < 10 ?'0' + day : day);
			    }
			    //时间
			    if ( angular.uppercase(timeArr[0]) == 'H') {
		       		date += " " + time.getHours()
			    }else if( angular.uppercase(timeArr[0]) == 'HH' ){
			       	date += " " + (time.getHours() < 10 ?'0' + time.getHours() : time.getHours());
			    }
			    if ( angular.uppercase(timeArr[1]) == 'M') {
		       		date += ":" + time.getUTCMinutes()
			    }else if( angular.uppercase(timeArr[1]) == 'MM' ){
			       	date += ":" + (time.getUTCMinutes() < 10 ?'0' + time.getUTCMinutes() : time.getUTCMinutes());
			    }
			    if ( angular.uppercase(timeArr[2]) == 'S') {
		       		date += ":" + time.getUTCSeconds()
			    }else if( angular.uppercase(timeArr[2]) == 'SS' ){
			       	date += ":" + (time.getUTCSeconds() < 10 ?'0' + time.getUTCSeconds() : time.getUTCSeconds());
			    }
			}else{
				date += time.getUTCFullYear() + "-";
				date += (month < 10 ?'0' + month : month) + "-";
				date += (day < 10 ?'0' + day : day) + " ";
			}
		return date;
	}
})
